package com.ytjj.qmyx.supplychain.common.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class ConcurrentThreadGlobalConfig {
 
    @Bean("excelThreadPool")
    public ThreadPoolTaskExecutor defaultThreadPool(){
        // 创建线程池
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 核心线程数
        executor.setCorePoolSize(Runtime.getRuntime().availableProcessors()+1);
        // 最大线程数
        executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors()+1);
        // 队列中的最大数目
        executor.setQueueCapacity(600);
        // 线程名称的前缀
        executor.setThreadNamePrefix("defaultThreadPool_");
        // 线程拒绝策略，由调用者决定
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 空闲线程时间
        executor.setKeepAliveSeconds(60);
        // 加载线程
        executor.initialize();
        return executor;
    }

}